﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

namespace WSweb
{
    public partial class Admin : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Header.Title = "WSweb - Admin";
            if ((string)Session["korisnik"] != "admin")
            {
                Response.Redirect("Nedozvoljena_autorizacija.aspx");
            }
            Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetNoStore();
        }

        protected void btnOcisti_Click(object sender, EventArgs e)
        {
            txt_imeVijesti.Text = "";
            txt_sadrzajVijesti.Text = "";
        }

        protected void btnUnesi_Click(object sender, EventArgs e)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("<div style='border:2px dashed #aeaeae; padding:0 15px 15px 15px;'>");
            sb.Append("<span style='font-size:30px;'>" + txt_imeVijesti.Text + "</span>");
            sb.Append("<p>  <img src='images/no_image.gif' height='100px' alt='no image' /></p>");
            sb.Append("<p>" + txt_sadrzajVijesti.Text + "</p>");
            sb.Append("<span style='font-size:12px;font-style:italic;color:#aeaeae;'>Dodano: " +
                DateTime.Now.ToString() + " | " + Session["korisnik"].ToString() + "</span>");
            sb.Append("</div>");
            ltlVijesti.Text = sb.ToString();
            
        }

        protected void btnSpremi_Click(object sender, EventArgs e)
        {
            int image_id = 1;
            int image_height = 200;
            SqlConnection c = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["DBwswebCS"].ConnectionString);
            c.Open();
            SqlCommand comm,comm2;
            string image_type = "image/gif";
            
            comm = new SqlCommand("insert into Vijest values (@naziv,@sadrzaj,@datum,@username,@image,@image_title,@image_id,@image_type,@image_height)", c);
            byte[] imageBytes = new byte[fuImage.PostedFile.InputStream.Length + 1];
            fuImage.PostedFile.InputStream.Read(imageBytes, 0, imageBytes.Length);
            if (fuImage.HasFile != false)
            {
                string fileName = Server.HtmlEncode(fuImage.FileName);
                string extension = System.IO.Path.GetExtension(fileName);
                System.Drawing.Image UploadedImage = System.Drawing.Image.FromStream(fuImage.PostedFile.InputStream);
                image_height = (int)UploadedImage.PhysicalDimension.Height;
                if (extension.EndsWith("png"))
                    image_type = "image/png";
                else if (extension.EndsWith("jpg"))
                    image_type = "image/jpg";
                else if (extension.EndsWith("bmp"))
                    image_type = "image/bmp";
                else
                    image_type = "image/gif";
                comm2 = new SqlCommand("select image_id from Vijest order by 1 desc", c);
                image_id = (int)comm2.ExecuteScalar() + 1;
            }
            comm.Parameters.AddWithValue("@naziv", txt_imeVijesti.Text);
            comm.Parameters.AddWithValue("@sadrzaj", txt_sadrzajVijesti.Text);
            comm.Parameters.AddWithValue("@datum", DateTime.Now);
            comm.Parameters.AddWithValue("@username", Session["korisnik"].ToString());
            comm.Parameters.AddWithValue("@image", imageBytes);
            comm.Parameters.AddWithValue("@image_title", fuImage.PostedFile.FileName);
            comm.Parameters.AddWithValue("@image_id",image_id);
            comm.Parameters.AddWithValue("@image_type",image_type);
            comm.Parameters.AddWithValue("@image_height",image_height);
            int broj = comm.ExecuteNonQuery();
            if ( broj == 0)
            {
                ltlVijesti.Text = "Neuspješno dodavanje vijesti u bazu!";
            }
            c.Close();
        }
    }
}